package cn.com.czpb.client.module.news.fragment;

import android.content.ContentValues;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import cn.com.czpb.client.R;
import cn.com.czpb.client.app.BaseFragment;
import cn.com.czpb.client.custorm.editcolumn.DragGridView;
import cn.com.czpb.client.model.event.ChannelSortedList;
import cn.com.czpb.client.model.news.NewsChannelBean;
import cn.com.czpb.client.module.news.adapter.DragAdapter;
import cn.com.czpb.client.utils.LogUtils;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.greenrobot.event.EventBus;

/**
 * Created by 猪猪侠 on 2017/8/3.
 */

public class NewsChannelEditFragment extends BaseFragment {
    @BindView(R.id.choise_sv)
    DragGridView choiseSv;
    @BindView(R.id.nochoise_sv_1)
    GridView nochoiseSv1;
    @BindView(R.id.tv_my_complete_id)//编辑
            TextView tvMyCompleteId;
    @BindView(R.id.img_my_close_id)//关闭
            ImageView imgMyCloseId;
    Unbinder unbinder;
    //适配器内容
    private List<NewsChannelBean> selectList;//上面的
    private List<NewsChannelBean> unSelectList0;//下面的
    private ChannelSortedList csl;
    private DragAdapter selectAdapter;    //上面的
    private DragAdapter unSelectAdapter0;//下面的适配器

    @Override
    public int setMyContentView() {
        return R.layout.fragment_channeledit;
    }

    @Override
    public void initView() {

    }


    @Override
    public void initData() {
        csl = new ChannelSortedList();
        selectList = new ArrayList<>();
        unSelectList0 = new ArrayList<>();
        selectList = DataSupport.where("ischoice=?", "1").order("sortOrder").find(NewsChannelBean.class);
        LogUtils.i("selectList-->" + selectList.size());
        unSelectList0 = DataSupport.where("ischoice=?", "0").order("sortOrder").find(NewsChannelBean.class);
        LogUtils.i("unSelectList0-->" + unSelectList0.size());
        setUpView();
    }

    private void setUpView() {
        unSelectAdapter0 = new DragAdapter(activity, unSelectList0, 1);
        selectAdapter = new DragAdapter(activity, selectList, 0);
        choiseSv.setAdapter(selectAdapter);//绿色的
        nochoiseSv1.setAdapter(unSelectAdapter0);
        choiseSv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (selectAdapter.isEdit()){
                    return true;
                }else{
                    selectAdapter.setEdit(true);
                    tvMyCompleteId.setText("完成");
                    selectAdapter.notifyDataSetChanged();
                    return false;
                }
            }
        });

        choiseSv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, final View view,
                                    final int position, long id) {
                if (selectAdapter.isEdit()){
                    if (position == 0) {
                        return;
                    }
                    final NewsChannelBean ncb = selectAdapter.getAdapterData().get(position);
                    LogUtils.i("category-->" + ncb.getCnname());
                    selectAdapter.deleteDataAtPosition(position);
                    unSelectAdapter0.addDataAtPosition(ncb, 0);
                    ContentValues values = new ContentValues();
                    values.put("ischoice", "0");
                    DataSupport.updateAll(NewsChannelBean.class, values, "tid=?", ncb.getTid());
                }else{
                    return;
                }

            }
        });
        nochoiseSv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, final View view,
                                    final int position, long id) {
                LogUtils.i("position-->" + position);
                if (selectAdapter.isEdit()) {
                    final NewsChannelBean ncb = unSelectAdapter0.getAdapterData().get(position);
                    LogUtils.i("category-->" + ncb.getCnname());
                    unSelectAdapter0.deleteDataAtPosition(position);
                    selectAdapter.addDataAtPosition(ncb, -1);
                    ContentValues values = new ContentValues();
                    values.put("ischoice", "1");
                    DataSupport.updateAll(NewsChannelBean.class, values, "tid=?", ncb.getTid());
                }
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.tv_my_complete_id, R.id.img_my_close_id})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_my_complete_id:
                if (selectAdapter.isEdit()){
                    tvMyCompleteId.setText("编辑");
                    selectAdapter.setEdit(false);
                    selectAdapter.notifyDataSetChanged();
                }else{
                    tvMyCompleteId.setText("完成");
                    selectAdapter.setEdit(true);
                    selectAdapter.notifyDataSetChanged();
                }


                break;
            case R.id.img_my_close_id:
                getActivity().onBackPressed();
                break;
        }
    }

    @Override
    public void onDestroy() {
        //选中的标题
        List<NewsChannelBean> list = selectAdapter.getAdapterData();
        for (int i = 0; i < list.size(); i++) {
            ContentValues values = new ContentValues();
            values.put("sortOrder",i);
            DataSupport.updateAll(NewsChannelBean.class, values, "tid=?",  list.get(i).getTid());
        }
        csl.setSaveTitleList(list);
        //未选中的标题推荐
        List<NewsChannelBean> list0 = unSelectAdapter0.getAdapterData();
        for (int i = 0; i < list0.size(); i++) {
            ContentValues values = new ContentValues();
            values.put("sortOrder","1000"+i);
            DataSupport.updateAll(NewsChannelBean.class, values, "tid=?",  list0.get(i).getTid());
        }
        EventBus.getDefault().post(csl);
        super.onDestroy();
    }
}
